package com.baoyouqun.controller.weixin;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baoyouqun.base.PageResponse;
import com.baoyouqun.domain.CreateCmd.UserPointsCreateCmd;
import com.baoyouqun.domain.Query.UserPointsQuery;
import com.baoyouqun.domain.VO.UserPointsVO;
import com.baoyouqun.service.UserPointsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("wx/userPoints")
@Api(tags = "用户积分表管理接口")
public class WxUserPointsController {

    @Resource
    private UserPointsService userPointsService;

    @GetMapping("page")
    @ApiOperation("分页查询用户积分表")
    public PageResponse<UserPointsVO> page(UserPointsQuery query) {
        String userId = StpUtil.getLoginIdAsString();
        query.setUserId(userId);

        UserPointsVO userPointsVO = userPointsService.getByUserId(userId);
        if (userPointsVO == null) {
            UserPointsCreateCmd userPointsCreateCmd = new UserPointsCreateCmd();
            userPointsCreateCmd.setUserId(userId);
            userPointsService.create(new UserPointsCreateCmd());
        }
        IPage<UserPointsVO> page = userPointsService.pageVo(query);
        return PageResponse.of(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
    }
}